<?php
	ob_start();
	session_start();

	function __autoload($class_name) {
		include './'.$class_name . '.php';
	}
	
	$result = array();
	$couponCode=$_GET['couponCode'];	
	$status = true;	
	$dbConfig = new DBConfigNew(); 
	$conn = $dbConfig->getConnection();
	$couponCode = stripslashes($couponCode);
	$couponCode = mysql_real_escape_string($couponCode);
	$query = null;

	$cart = NULL;
	if(!isset($_SESSION['cart'])){
		$results['#success'] = false;
		$status = false;
	}else {
		$cart = $_SESSION['cart'];
	}
	
	if($status && is_null($couponCode)) {
		$status = false;
	}
	
	if($status) {
		$query = "CALL hambaaco_merchandize.validate_coupon_q('".$couponCode."',null,null);";
		$conn->multi_query($query);	
		if ($result = $conn->store_result()) {
			$row = $result->fetch_array();
			if (!is_null($row['cd_type'])){
				$cart->setCouponCode($couponCode);
				$results['coupon']='Y';
			} else {
				$results['coupon']='N';
				$status = false;
			}		
		}
			
	}
	
	if ($status){		
		header("Location: shoppingcart.php");		
	} else {
		$results['#success'] = false;
	}

	echo json_encode($results);
	
	ob_end_flush();
?>